'''
Author: momochong0
Date: 2021-06-28 18:02:10
LastEditors: momochong0
LastEditTime: 2021-07-01 10:44:17
Description: 要推网络荣誉出品
'''
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import csv
import time


# def getOnePageJob( driver, writer ):
#     jobInfos = driver.find_elements_by_class_name('job-primary')
#     for info in jobInfos:
#         print( info.text)
#         strText = info.text
#         arrTxt = strText.split("\n")
#         tmp = arrTxt[2].split("-")
#         arrTxt.insert( 2, tmp[0] )
#         writer.writerow(arrTxt)

def getOnePageJob( driver, writer ):
    jobInfos = driver.find_elements_by_class_name('primary-box')
    main_title=driver.title
    main_handle=driver.current_window_handle
    for info in jobInfos:
        print( info.get_attribute('href'))
        title=info.find_element_by_class_name('job-name').text
        print(title)
        info.click()
        allHandles = driver.window_handles
        for handle in allHandles:
            if handle==main_handle:
                continue
            driver.switch_to.window(handle)
            print(handle,driver.title,driver.title.find(main_title))
                # jobContent = driver.find_elements_by_class_name('detail-content')
                # for job in jobContent:
                #     job_detail=job.find_element_by_xpath("/div[1]/div[1]")
                #     print(job_detail.text)

        # driver.implicitly_wait(30)

        # des_lst = info.find_elements_by_xpath('//*[@class="detail-content"]')
        print(driver.title)
        # time.sleep(30)
        # des = [x.text for x in des_lst]
        # print(des)
        
        # subiframe2 = driver.find_elements_by_class_name('detail-content')
        # driver.switch_to.window(subiframe2)
        # print(driver.page_source)
        # jobContent = driver.find_elements_by_class_name('detail-content')
        # for job in jobContent:
        #     job_detail=job.find_element_by_xpath("/div[1]/div[1]")
        #     print(job_detail)
        driver.close()
        driver.switch_to.window(main_handle)
        print(driver.title)


url='https://www.zhipin.com/beijing/'

driver = webdriver.Chrome()
driver.get(url)

#找到输入框
searchEle = driver.find_element_by_class_name('ipt-search')
searchEle.send_keys('爬虫工程师')

#点击搜索按钮
btnSearch = driver.find_element_by_css_selector('button.btn.btn-search')
# btnSearch = driver.find_element_by_class_name('btn btn-search')
btnSearch.click()

i =1 
while i < 5 :
    csvfile = open("data.csv", 'a')
    writer = csv.writer(csvfile)

    getOnePageJob(driver,writer)
    i = i + 1
    driver.implicitly_wait(30)
    btnnext = driver.find_element_by_css_selector("a.next")
    btnnext.click()

    csvfile.close()